r"""
 setup.py

 Tutt Library Systems Task Planner Set-up

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 Copyright Jeremy Nelson, Colorado College
 """
__author__ = "Jeremy Nelson"
__version__ = "0.1.1"

import code,datetime
import getpass,logging
import sys,os

#path_to_appengine = 'C:\\Program Files\\Google\\'
path_to_appengine = '/Users/jeremynelson/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/'
sys.path.append(os.path.join(path_to_appengine,
                             'google_appengine'))
sys.path.append(os.path.join(path_to_appengine,
                             'google_appengine/lib/yaml/lib'))
sys.path.append(os.path.join(path_to_appengine,
                             'google_appengine/lib/fancy_urllib'))

from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
from google.appengine.api import users
from models import Log,Person,Project,Role,System,Task

def auth_func():
    return raw_input('Username:'),getpass.getpass('Password:')

def clear_datastore():
    confirm = raw_input(r"Are you sure you want to clear the datastore? (Y|N)")
    if confirm.upper() == 'Y':
        all_projects = Project.all()
        for project in all_projects:
            name = project.name
            project.delete()
            print("\tDeleted Project %s" % name)
        all_tasks = Task.all()
        for task in all_tasks:
            name = task.description
            task.delete()
            print("\tDeleted Task %s" % name)
        all_roles = Role.all()
        for role in all_roles:
            name = role.description
            role.delete()
            print("\tDeleted Role %s" % name)
        people = Person.all()
        for person in people:
            name = "%s %s" % (person.given,person.family)
            person.delete()
            print("\t%s deleted" % name)
        systems = System.all()
        for system in systems:
            name = system.name
            system.delete()
            print("\t%s deleted" % name)
        logs = Log.all()
        for log in logs:
            comment = log.description
            log.delete()
            print("\t%s log deleted" % comment)
        print("Finished clearing datastore at %s" % datetime.datetime.today().isoformat())
        

def create_cc():
    print("Creates initial users")
    jerm_user = users.User('jermnelson@gmail.com')
    cindy = Person(cc_email='ctappan@coloradocollege.edu',
                   family='Tappan',
                   given='Cindy',
                   created_by=jerm_user)
    cindy.put()
    diane = Person(cc_email='diane.westerfield@coloradocollege.edu',
                   family='Westerfield',
                   given='Diane',
                   created_by=jerm_user)
    diane.put()
    christine = Person(cc_email='cashlock@coloradocollege.edu',
                       family='Ashlock',
                       given='Christine',
                       created_by=jerm_user)
    christine.put()
    jeremy = Person(cc_email='jeremy.nelson@coloradocollege.edu',
                    family='Nelson',
                    given='Jeremy',
                    login=jerm_user,
                    created_by=jerm_user)
    jeremy.put()
    jon = Person(cc_email='jdriscoll@coloradocollege.edu',
                 family='Driscoll',
                 given='Jon',
                 login=users.User('drisjon@gmail.com'),
                 created_by=jerm_user)
    jon.put()

    # Creates initial Roles
    print("Creates initial Roles")
    developer = Role(description='Software Development',
                     people=[jeremy.key(),
                             diane.key(),
                             cindy.key(),
                             jon.key()])
    developer.put()
    marc_improve = Role(description="MARC Record Improvement",
                        people=[christine.key(),
                                cindy.key()])
    marc_improve.put()
    resource_manager = Role(description="Vendor Resource Management",
                            people=[diane.key(),
                                    jon.key()])
    resource_manager.put()
    sys_admin = Role(description='Tutt Internal System Administration',
                     people=[jeremy.key(),
                             jon.key()])
    sys_admin.put()
    web_developer = Role(description='HTML and Web Development',
                         people=[jeremy.key(),
                                 diane.key(),
                                 jon.key(),
                                 christine.key()])
    web_developer.put()
    # Initial Project and Tasks lists as of 5/2011
    print("Initial Project and Tasks lists as of 5/2011")
    dacc_spotchk = Task(assigned_to=[jeremy.key(),
                                     christine.key()],
                        created_by=jerm_user,
                        description='P-DACC Compliance check',
                        start_date=datetime.datetime(year=2011,
                                                     month=4,
                                                     day=1),
                        end_date=datetime.datetime(year=2011,
                                                   month=6,
                                                   day=1),
                        tags=[db.Category('dacc'),])
    dacc_spotchk.put()
    dacc_log1 = Log(created_by=jerm_user,
                    days=[datetime.datetime(year=2011,
                                                month=5,
                                                day=18),],
                    description='Creation',
                    entities=[dacc_spotchk.key(),],
                    tags=dacc_spotchk.tags)
    dacc_log1.put()
    dacc_release = Task(created_by=jerm_user,
                        description='Colorado Alliance DACC Upgrade Release',
                        is_milestone=True,
                        start_date=datetime.datetime(year=2011,
                                                     month=4,
                                                     day=1),
                        estimate_finish_date=datetime.datetime(year=2011,
                                                               month=6,
                                                               day=12),
                        tags=[db.Category('dacc'),
                              db.Category('web-framework')])
    dacc_release.put()
    dacc_log2 = Log(created_by=jerm_user,
                    days=[datetime.datetime(year=2011,
                                                month=5,
                                                day=18),],
                    description='Creation',
                    entities=[dacc_release.key(),],
                    tags=dacc_release.tags)
    dacc_log2.put()
    ccetd_update = Task(assigned_to=[jeremy.key(),],
                        created_by=jerm_user,
                        days=[datetime.datetime.today(),],
                        description="Update CCETD to Pyramid and new DACC",
                        start_date=datetime.datetime(year=2011,
                                                     month=6,
                                                     day=12),
                        estimate_finish_date=datetime.datetime(year=2011,
                                                               month=7,
                                                               day=15),
                        tags=[db.Category('dacc'),
                              db.Category('pyramid'),
                              db.Category('ccetd')])
    ccetd_update.put()
    ccetd_log = Log(created_by=jerm_user,
                    description='Creation',
                    days=[datetime.datetime(year=2011,month=5,day=18),],
                    entities=[ccetd_update.key(),],
                    tags=ccetd_update.tags)
    ccetd_log.put()
    dacc_project = Project(created_by=jerm_user,
                           name='DACC Islandora/Fedora Upgrade',
                           start_date=datetime.datetime(year=2011,
                                                        month=4,
                                                        day=1),
                           end_date=datetime.datetime(year=2011,
                                                      month=6,
                                                      day=12),
                           tasks=[dacc_spotchk.key(),
                                  dacc_release.key(),
                                  ccetd_update.key()])
    dacc_project.put()
    vm_setup = Task(assigned_to=[jeremy.key(),
                                 jon.key()],
                    created_by=jerm_user,
                    description='Setup VM for dedicated Solr server',
                    estimate_finish_date=datetime.datetime(year=2011,
                                                           month=5,
                                                           day=25),
                    skills=[sys_admin.key(),],
                    tags=[db.Category('solr'),
                          db.Category('system')])
    vm_setup.put()
    vmset_log = Log(created_by=jerm_user,
                    description='Creation',
                    days=[datetime.datetime(year=2011,month=5,day=18),],
                    entities=[vm_setup.key(),],
                    tags=vm_setup.tags)
    vmset_log.put()
    facets = Task(assigned_to=[jeremy.key(),
                               jon.key(),
                               christine.key()],
                  created_by=jerm_user,
                  description='Facets for Solr',
                  start_date=datetime.datetime(year=2011,
                                               month=4,
                                               day=1),
                  estimate_finish_date=datetime.datetime(year=2011,
                                                         month=7,
                                                         day=30),
                  skills=[developer.key(),
                          marc_improve.key()],
                  tags=[db.Category('solr'),
                        db.Category('blacklight')])
    facets.put()

    facets_log = Log(created_by=jerm_user,
                     description='Creation',
                     days=[datetime.datetime(year=2011,month=5,day=18),],
                     entities=[facets.key(),],
                     tags=facets.tags)
    facets_log.put()
    solr_project = Project(created_by=jerm_user,
                           name='Solr standalone server',
                           start_date=datetime.datetime(year=2011,
                                                        month=4,
                                                        day=1),
                           end_date=datetime.datetime(year=2011,
                                                      month=5,
                                                      day=30),
                           skills=[sys_admin.key(),
                                   marc_improve.key()],
                           tasks=[vm_setup.key(),
                                  facets.key()])   
    solr_project.put()
    pyramid_setup = Task(assigned_to=[jeremy.key(),],
                         created_by=jerm_user,
                         description="Setup Pyramid Web Application Development Framework",
                         estimate_finish_date=datetime.datetime(year=2011,
                                                                month=5,
                                                                day=20),
                         skills=[sys_admin.key(),
                                 web_developer.key()],
                         tags=[db.Category('pyramid'),
                               db.Category('web-framework')])
    pyramid_setup.put()
    pyramid_log = Log(created_by=jerm_user,
                      description='Creation',
                      days=[datetime.datetime(year=2011,month=5,day=10),],
                      entities=[pyramid_setup.key(),],
                      tags=pyramid_setup.tags)
    pyramid_log.put()
    grx_bot = Task(assigned_to=[jeremy.key(),],
                   created_by=jerm_user,
                   description="Gold Rush Web Services Bot",
                   estimate_finish_date=datetime.datetime(year=2011,
                                                          month=5,
                                                          day=20),
                   skills=[developer.key(),],
                   tags=[db.Category('gold rush'),
                         db.Category('bot')])
    grx_bot.put()
    grx_bot_log = Log(created_by=jerm_user,
                      description='Creation',
                      days=[datetime.datetime(year=2011,month=5,day=10),],
                      entities=[grx_bot.key(),],
                      tags=grx_bot.tags)
    grx_bot_log.put()
    solr_bot = Task(assigned_to=[jeremy.key(),
                                 christine.key()],
                    created_by=jerm_user,
                    description="Solr Search Bot",
                    estimate_finish_date=datetime.datetime(year=2011,
                                                           month=5,
                                                           day=20),
                    skills=[developer.key(),
                            marc_improve.key()],
                    tags=[db.Category('gold rush'),
                          db.Category('bot'),
                          db.Category('solr')])
    solr_bot.put()
    solr_bot_log = Log(created_by=jerm_user,
                       description='Creation',
                       days=[datetime.datetime(year=2011,month=5,day=10),],
                       entities=[solr_bot.key(),],
                       tags=solr_bot.tags)
    solr_bot_log.put()
    gold_rush_setup = Task(assigned_to=[diane.key(),],
                           created_by=jerm_user,
                           days=[datetime.datetime(year=2011,
                                            month=4,
                                            day=1)],
                           description="Colorado College Gold Rush Setup",
                           estimate_finish_date=datetime.datetime(year=2011,
                                                                  month=6,
                                                                  day=30),
                           skills=[resource_manager.key(),],
                           tags=[db.Category('gold rush'),])
    gold_rush_setup.put()
    gold_rush_log = Log(created_by=jerm_user,
                        description='Creation',
                        days=[datetime.datetime(year=2011,month=4,day=1),],
                        entities=[gold_rush_setup.key(),],
                        tags=gold_rush_setup.tags)
    gold_rush_log.put()
    grxms_project = Project(created_by=jerm_user,
                            dependencies=[solr_project.key(),],
                            name='Gold Rush Microservices',
                            start_date=datetime.datetime(year=2011,
                                                     month=5,
                                                     day=1),
                            end_date=datetime.datetime(year=2011,
                                                       month=6,
                                                       day=15),
                            tasks=[pyramid_setup.key(),
                                   gold_rush_setup.key(),
                                   grx_bot.key(),
                                   solr_bot.key()])
    grxms_project.put()
    dotcms = Task(created_by=jerm_user,
                  description='Colorado College dotCMS Availability',
                  is_milestone=True,
                  start_date=datetime.datetime(year=2011,
                                           month=4,
                                           day=1),
                  estimate_finish_date=datetime.datetime(year=2011,
                                                     month=6,
                                                     day=30),
                  tags=[db.Category('dotcms'),
                        db.Category('web-framework')])
    dotcms.put()
    dotcms_log = Log(created_by=jerm_user,
                     description='Creation',
                     days=[datetime.datetime(year=2011,month=4,day=1),],
                     entities=[dotcms.key(),],
                     tags=dotcms.tags)
    dotcms_log.put()
    ww_design = Task(created_by=jerm_user,
                     description='White Whale Library Website Design',
                     is_milestone=True,
                     start_date=datetime.datetime(year=2011,
                                              month=4,
                                              day=12),
                     estimate_finish_date=datetime.datetime(year=2011,
                                                        month=6,
                                                        day=30),
                     tags=[db.Category('web-design'),])
    ww_design.put()
    ww_design_log = Log(created_by=jerm_user,
                        description='Creation',
                        days=[datetime.datetime(year=2011,month=4,day=12),],
                        entities=[ww_design.key(),],
                        tags=ww_design.tags)
    ww_design_log.put()
    migrate_pages = Task(assigned_to=[jeremy.key(),
                                      jon.key(),
                                      diane.key(),
                                      christine.key()],
                         created_by=jerm_user,
                         dependencies=[dotcms.key(),],
                         start_date=datetime.datetime(year=2011,
                                                  month=5,
                                                  day=1),
                         description="Migrate Content to dotCMS",
                         estimate_finish_date=datetime.datetime(year=2011,
                                                            month=7,
                                                            day=10),
                         skills=[developer.key(),
                                 sys_admin.key(),
                                 web_developer.key()],
                         tags=[db.Category('web-design'),
                               db.Category('dotcms'),
                               db.Category('library-website')])
    migrate_pages.put()
    migrate_log = Log(created_by=jerm_user,
                      description='Creation',
                      days=[datetime.datetime(year=2011,month=5,day=18),],
                      entities=[migrate_pages.key(),],
                      tags=migrate_pages.tags)
    migrate_log.put()
    migrate_to_libguides = Task(assigned_to=[jeremy.key(),
                                             diane.key()],
                                created_by=jerm_user,
                                description="Migrate instructional content to LibGuides",
                                skills=[web_developer.key()],
                                tags=[db.Category('library-website'),
                                      db.Category('libguides')],)
    migrate_to_libguides.put()
    migrate_lib_log = Log(created_by=jerm_user,
                          description='Creation',
                          days=[datetime.datetime(year=2011,month=5,day=18),],
                          entities=[migrate_to_libguides.key(),],
                          tags=migrate_to_libguides.tags)
    migrate_lib_log.put()

    lib_website = Project(created_by=jerm_user,
                          dependencies=[dotcms.key(),
                                        grxms_project.key()],
                          name='Tutt Library New Website',
                          start_date=datetime.datetime(year=2011,
                                                   month=4,
                                                   day=12),
                          end_date=datetime.datetime(year=2011,
                                                 month=7,
                                                 day=15),
                          tasks=[ww_design.key(),
                                 migrate_pages.key(),
                                 migrate_to_libguides.key()])
    lib_website.put()
    discovery_marc = Task(assigned_to=[christine.key(),
                                       cindy.key()],
                          created_by=jerm_user,
                          start_date=datetime.datetime(year=2011,
                                                   month=4,
                                                   day=1),
                          description="MARC Record QA for Discovery System",
                          skills=[marc_improve.key(),],
                          tags=[db.Category('marc'),
                                db.Category('discovery-system')])
    discovery_marc.put() 
    marc_log = Log(created_by=jerm_user,
                   description='Creation',
                   days=[datetime.datetime(year=2011,month=5,day=18),],
                   entities=[discovery_marc.key(),],
                   tags=discovery_marc.tags)
    marc_log.put()
    ror_3 = Task(assigned_to=[jeremy.key(),
                              jon.key()],
                 created_by=jerm_user,
                 start_date=datetime.datetime(year=2011,
                                          month=5,
                                          day=20),
                 description="Install Ruby-on-Rails 3",
                 estimate_finish_date=datetime.datetime(year=2011,
                                                    month=6,
                                                    day=15),
                 skills=[developer.key(),
                         sys_admin.key(),
                         web_developer.key()],
                 tags=[db.Category('web-design'),
                       db.Category('blacklight'),
                       db.Category('discovery-system')])
    ror_3.put()
    ror3_log = Log(created_by=jerm_user,
                   description='Creation',
                   days=[datetime.datetime(year=2011,month=5,day=18),],
                   entities=[ror_3.key(),],
                   tags=ror_3.tags)
    ror3_log.put()
    vm_setup2 = Task(assigned_to=[jeremy.key(),
                                 jon.key()],
                     created_by=jerm_user,
                     days=[datetime.datetime.today(),],
                     description='Setup VM for production Blacklight server',
                     estimate_finish_date=datetime.datetime(year=2011,
                                                        month=6,
                                                        day=15),
                     skills=[sys_admin.key(),],
                     tags=[db.Category('blacklight'),
                           db.Category('system')])
    vm_setup2.put()
    vm2s_log = Log(created_by=jerm_user,
                   description='Creation',
                   days=[datetime.datetime(year=2011,month=5,day=18),],
                   entities=[vm_setup2.key(),],
                   tags=vm_setup2.tags)
    vm2s_log.put()
    discover_layer_project = Project(created_by=jerm_user,
                                     dependencies=[solr_project.key(),],
                                     name="Tutt Library Blacklight Discovery Layer",
                                     start_date=datetime.datetime(year=2011,
                                                              month=4,
                                                              day=1),
                                     end_date=datetime.datetime(year=2011,
                                                            month=8,
                                                            day=15),
                                     tasks=[ww_design.key(),
                                            ror_3.key(),
                                            vm_setup2.key(),
                                            discovery_marc.key()])
    discover_layer_project.put()
    print("Finished cc inital setup")

def create_cc_systems():
    blacklight = System(name='Blacklight Discovery Layer Server',
                        src_repository_url=\
                        'http://code.google.com/p/tuttlibrarysystems/source/checkout',
                        url='http://tuttdemo.coloradocollege.edu:3000',)
    blacklight.put()
    ccetd = System(name='CCETD Service',
                   src_repository_url=\
                   'http://code.google.com/p/tuttlibrarysystems/source/checkout?repo=ccetd',
                   url='http://tuttdemo.coloradocollege.edu/ccetd')
    ccetd.put()
    goldrush = System(name='Gold Rush Microservices',
                      src_repository_url=\
                      'http://code.google.com/p/tuttlibrarysystems/source/checkout?repo=goldrush',
                      url='http://tuttdemo.coloradocollege.edu/grx')
    goldrush.put()
    solr = System(name='Solr Index Server',
                  src_repository_url=\
                  'http://code.google.com/p/tuttlibrarysystems/source/checkout?repo=solr',
                  url='http://tuttdemo.coloradocollege.edu:8983/solr')
    solr.put()
    tiger = System(name='TIGER Millennium OPAC',
                   url='http://tiger.coloradocollege.edu')
    tiger.put()
    website = System(name='Tutt Library Main Website',
                     url='http://www.coloradocollege.edu/library')
    website.put()
                                     


if __name__ == '__main__':
    app_id = 'tuttsystems'
    print("Tutt Systems Utilities")
    datastore_location = raw_input("Local or remote? (L|R)")
    if datastore_location.lower() == 'l':
        host = 'localhost:8081'
    elif datastore_location.lower() == 'r':
        host = '%s.appspot.com' % app_id
    else:
        print("UNKNOWN host %s" % datastore_location.lower())
    remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
    code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
    
    
                              


                   
